﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EM.Entities;
using Microsoft.Office.Interop.Excel;
using System.IO;
using System.Reflection;

namespace EM_Utils
{
    public class UTExcel
    {
        Application appExcel;  //Aplicacìón de Excel
        Workbook docExcel;  //Libro
        Worksheet shtExcel;  //Hoja


        public bool GeneraReporte(EM_Cabecera cabecera, string chartPath)
        {
        

            string sPath = System.IO.Path.GetTempFileName();
            
            System.IO.File.WriteAllBytes(sPath, Properties.Resources.PlantillaEM);
            object missvalue = System.Reflection.Missing.Value;

           
            //string valor = null;
            //double operador1 = 0;
            //double operador2 = 0;
            //double result = 0;
            appExcel = new Application();
            docExcel = appExcel.Workbooks.Open(sPath,0,true,5,"","",true,XlPlatform.xlWindows,"\t",true,false,0,true,1,0);

          
            shtExcel = docExcel.Sheets[4];
            //Selecciona la primera hoja

            //for (int i = 1; i <= shtExcel.Rows.Count - 1; i++)
            //{
            //    operador1 = 0;
            //    operador2 = 0;
            //    result = 0;
            //    valor = shtExcel.get_Range(shtExcel.Cells[i, 1], shtExcel.Cells[i, 1]).Value;

            //    if (string.IsNullOrEmpty(Strings.Trim(valor)))
            //    {
            //        break; // TODO: might not be correct. Was : Exit For
            //    }
            //    if (Information.IsNumeric(valor))
            //    {
            //        operador1 = Convert.ToDouble(valor);
            //        valor = shtExcel.Range(shtExcel.Cells(i, 2), shtExcel.Cells(i, 2)).Value;
            //        operador2 = Convert.ToDouble(valor);
            //        result = operador1 * operador2;

            //        shtExcel.Cells(i, 3) = result;
            //    }

            //}

            
            //for (int i = 1; i <= shtExcel.Rows.Count - 1; i++)
            //{
            //    operador1 = 0;
            //    operador2 = 0;
            //    result = 0;
            //    valor = shtExcel.Range(shtExcel.Cells(i, 3), shtExcel.Cells(i, 3)).Value;

            //    if (string.IsNullOrEmpty(Strings.Trim(valor)))
            //    {
            //        break; // TODO: might not be correct. Was : Exit For
            //    }

            //    Interaction.MsgBox(valor);

            //}
            
            shtExcel.Cells[6, 3].Value = cabecera.nomProducto;
            shtExcel.Cells[10, 3].Value = cabecera.tipoEmpaque;
            shtExcel.Cells[13, 4].Value = cabecera.nroLotes;
            shtExcel.Cells[14, 4].Value = cabecera.motivo;
            shtExcel.Cells[15, 4].Value = cabecera.obervacion;
            shtExcel.Cells[18, 4].Value = cabecera.activoVal;
            shtExcel.Cells[19, 4].Value = cabecera.paramEval;
            shtExcel.Cells[20, 4].Value = cabecera.tipoAnalisis;

            shtExcel.Cells[25, 3].Value = cabecera.strLote_1;
            shtExcel.Cells[26, 3].Value = cabecera.strLote_2;
            shtExcel.Cells[27, 3].Value = cabecera.strLote_3;

            shtExcel.Cells[25, 8].Value = cabecera.LimEspecif;
            shtExcel.Cells[27, 8].Value = cabecera.humedadRel;


            int i = 34;
         
            foreach(EM_Detalle objDet in cabecera.Detalle){

                shtExcel.Cells[i, 2].Value = objDet.tiempo;
                shtExcel.Cells[i, 3].Value = objDet.muestra;
                shtExcel.Cells[i, 4].Value = objDet.fechaLt1;
                shtExcel.Cells[i, 5].Value = objDet.valorLt1;
                shtExcel.Cells[i, 6].Value = objDet.fechaLt2;
                shtExcel.Cells[i, 7].Value = objDet.valorLt2;
                shtExcel.Cells[i, 8].Value = objDet.fechaLt3;
                shtExcel.Cells[i, 9].Value = objDet.valorLt3;          
                                 
                i++;
            }
            //valor = shtExcel.get_Range(shtExcel.Cells[6, 1], shtExcel.Cells[6, 1]).Value;

            ChartObjects chartObjs = (ChartObjects)shtExcel.ChartObjects(Type.Missing);
            //ChartObject chart1 =(ChartObject) chartObjs.Item(2);//Convert.ChangeType(shtExcel.ChartObjects("Chart 3"), Type.GetType("Microsoft.Office.Interop.Excel.Chart"));
            //ChartObject chart1 = (ChartObject)chartObjs.Item("Chart 3");
            ChartObject chart1 = (ChartObject)chartObjs.Item("Chart 3");
            Chart chartPage = chart1.Chart;
           
            chartPage.CopyPicture();

            chartPage.Export(chartPath, missvalue);
            cabecera.grafico = chartPage;

            
            //appExcel.SaveWorkspace()
            //shtExcel = null;
            //docExcel.SaveAs("C://%temp%/prueba2.xls");
            //docExcel.Close();
            //docExcel.
            appExcel.Visible = true;
            docExcel = null;
            ////appExcel = Nothing
            //appExcel.Quit();
            appExcel = null;
            //appExcel.pro

            return true;
        }

    }


}
